import request from '@/utils/request'
import md5 from 'js-md5'
import { randomString } from '@/utils/index'

const secret = '127209E2A56EAB7B9DA68DB1E2CA11BB'
const appkey = 'UFNLT8DW'
function md5ByTime(params = {}) {
  const paramsStr = Object.keys(params).sort().map(key => `${key}=${params[key]}`).join('&')
  // const token = `appkey=${appkey}&nonce=${nonce}&time=${time}&secret=${secret}`
  return md5(`${paramsStr}&secret=${secret}`).toUpperCase()
}
const baseUrl = process.env.NODE_ENV === 'production' ? 'http://10.32.39.216:8092/cloud' : '/cloud'
export function subscribeEventsFromMQ(data = {}) {
  const time = Date.now()
  const nonce = randomString(32)
  const params = { nonce, appkey, time, ...data }
  const token = md5ByTime(params)

  return request({
    url: baseUrl + '/service/subscribeEventsFromMQ',
    method: 'post',
    params: { token },
    data: { ...params }
  })
}
export function getParkingInfos(data = {}) {
  const time = Date.now()
  const nonce = randomString(32)
  const params = { nonce, appkey, time, ...data }
  const token = md5ByTime(params)
  return request({
    url: baseUrl + '/service/getParkingInfos',
    method: 'post',
    params: { token },
    data: { ...params }
  })
}
export function getParkingInfosByParkCodes(data = {}) {
  const time = Date.now()
  const nonce = randomString(32)
  const params = { nonce, appkey, time, ...data }
  const token = md5ByTime(params)
  return request({
    url: baseUrl + '/service/getParkingInfosByParkCodes',
    method: 'post',
    params: { token },
    data: { ...params }
  })
}
export function getVehicleRecords(data = {}) {
  const time = Date.now()
  const nonce = randomString(32)
  const params = { nonce, appkey, time, ...data }
  const token = md5ByTime(params)
  return request({
    url: baseUrl + '/service/getVehicleRecords',
    method: 'post',
    params: { token },
    data: { ...params }
  })
}
